package org.zjx.action.game.inventor;

import org.zjx.analysis.flag.*;
import org.zjx.action.game.AbstractWindowAction;
import org.zjx.core.IWindow;

import java.awt.*;
import java.util.List;
import java.util.Map;

public class GatherFlagAction extends AbstractWindowAction {
    private final Map<String, Object> map;
    private final BaseFlagAnalysis baseFlagAnalysis;

    public GatherFlagAction(IWindow targetWindow, String cityName, Map<String, Object> map) {
        super(targetWindow);
        this.map = map;
        switch (cityName){
            case "长安城":
                baseFlagAnalysis = new FlagsCACAnalysis(targetWindow,targetWindow.getState().getGameState().getScreenshot());
                break;
            case "傲来国":
                baseFlagAnalysis = new FlagsALGAnalysis(targetWindow,targetWindow.getState().getGameState().getScreenshot());
                break;
            case "长寿村":
                baseFlagAnalysis = new FlagsCSCAnalysis(targetWindow,targetWindow.getState().getGameState().getScreenshot());
                break;
            case "朱紫国":
                baseFlagAnalysis = new FlagsZZGAnalysis(targetWindow,targetWindow.getState().getGameState().getScreenshot());
                break;
            default:
                throw new IllegalArgumentException("合成旗城市未收录:"+cityName);
        }
    }

    @Override
    public void open() {

    }

    @Override
    public void close() {
        Rectangle closeButton = baseFlagAnalysis.getCloseButtonPoint();
        targetWindow.execute(op->{op.click(closeButton.getCenterX(),closeButton.getCenterY());op.delay(300);return null;});
    }

    @Override
    public void reSetWindow() {

    }

    @Override
    public void mouseAloofWindow(Rectangle rect) {
        if (baseFlagAnalysis.mouseInRect(rect)) {
            Point safePoint = baseFlagAnalysis.findLargestRemainingAreaCenter(rect);
            move(safePoint);
        }
    }

    @Override
    protected void doExecute() throws Exception {
        mouseAloofWindow(baseFlagAnalysis.windowArea == null? baseFlagAnalysis.getOriginBounds(): baseFlagAnalysis.windowArea);
        List<Point> locations = baseFlagAnalysis.getLocations();
        map.put("points", locations);
        close();
    }
}
